package com.js.mytransitiondemo.customize;

// Created by JS on 2021/7/7.

import android.os.Build;
import android.os.Bundle;
import android.transition.Transition;
import android.transition.TransitionSet;
import android.view.View;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.js.mytransitiondemo.R;
import com.js.mytransitiondemo.databinding.ActivityGarageNewBinding;
import com.js.mytransitiondemo.transition.GarageEnterTransition;
import com.js.mytransitiondemo.transition.GarageReturnTransition;
import com.js.mytransitiondemo.transition.OtherTransition;
import com.js.mytransitiondemo.transition.PositionTransition;

public class GarageActivityNew extends AppCompatActivity {

    private ActivityGarageNewBinding binding;

    //为了看清楚动画过程设置为1500毫秒，经过试验发现300毫秒体验最好
    private int playTime = 300;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); // 设置启用窗口过渡属性;
        getWindow().setAllowEnterTransitionOverlap(false); // 就是禁止交叉，只有退出过渡动画结束后才会再显示进入过渡动画。
        getWindow().setAllowReturnTransitionOverlap(false); // 就是禁止交叉，只有退出过渡动画结束后才会再显示进入过渡动画。
        binding = ActivityGarageNewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setTransition();

        binding.carView.setOnClickListener(v -> {
            finishAfterTransition(); // 想要有过渡动画的退出效果，不能直接用finish()
        });
    }

    private void setTransition() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            OtherTransition otherTransition = new OtherTransition(this, binding.rootView, binding.carView);
            otherTransition.setDuration(playTime);
            getWindow().setEnterTransition(otherTransition);

            getWindow().setSharedElementEnterTransition(buildEnterTransition());
            getWindow().setSharedElementReturnTransition(buildReturnTransition());
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private TransitionSet buildEnterTransition() {
        TransitionSet transitionSet = new TransitionSet();

        Transition changePos = new PositionTransition();
        changePos.setDuration(playTime);
        changePos.addTarget(R.id.car);
        transitionSet.addTransition(changePos);

        GarageEnterTransition enterTransition = new GarageEnterTransition();
        enterTransition.addTarget(R.id.car);
        enterTransition.setDuration(playTime);
        transitionSet.addTransition(enterTransition);

        return transitionSet;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private TransitionSet buildReturnTransition() {
        TransitionSet transitionSet = new TransitionSet();

        Transition changePos = new PositionTransition();
        changePos.setDuration(playTime);
        changePos.addTarget(R.id.car);
        transitionSet.addTransition(changePos);

        GarageReturnTransition returnTransition = new GarageReturnTransition();
        returnTransition.addTarget(R.id.car);
        returnTransition.setDuration(playTime);
        transitionSet.addTransition(returnTransition);

        return transitionSet;
    }
}
